package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.DrugInfoMapper;
import com.ssy.bus.mapper.OrderMapper;
import com.ssy.bus.mapper.PurchaseMapper;
import com.ssy.bus.pojo.Order;
import com.ssy.bus.pojo.Purchase;
import com.ssy.bus.pojo.Supplier;
import com.ssy.bus.service.IPurchaseService;
import com.ssy.bus.service.ISupplierService;
import com.ssy.bus.vo.PurchaseVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DrugInfoMapper drugInfoMapper;

    @Override
    public PageResult loadAllPurchasePage(PurchaseVo purchaseVo) {
        Page<Purchase> page = new Page<>(purchaseVo.getPage(), purchaseVo.getLimit());
        IPage<Purchase> purchaseIPage = this.purchaseMapper.selectPage(page, new LambdaQueryWrapper<Purchase>()
                .eq(purchaseVo.getSupplierId() != null, Purchase::getSupplierId, purchaseVo.getSupplierId())
                .like(StringUtils.hasText(purchaseVo.getApplicant()), Purchase::getApplicant, purchaseVo.getApplicant())
                .eq(purchaseVo.getState() != null, Purchase::getState, purchaseVo.getState())
                .orderByDesc(Purchase::getCreateTime));

        List<Purchase> records = purchaseIPage.getRecords();
        //取出供应商名称
        for (Purchase record : records) {
            Integer supplierId = record.getSupplierId();
            Supplier supplier = supplierService.selectSupplierById(supplierId);
            record.setSupplierName(supplier.getName());
        }
        return new PageResult(purchaseIPage.getTotal(), records);
    }

    @Override
    public Result InsertPurchase(Purchase purchase) {
        if (purchase == null) {
            return new Result(CodeMsg.SUBMIT_APPLICATION_ERROR);
        }
        this.purchaseMapper.deleteById(purchase.getId());
        int i = this.purchaseMapper.insert(purchase);
        return i > 0 ? new Result(CodeMsg.PRESERVATION_SUCCESS) : new Result(CodeMsg.PRESERVATION_ERROR);
    }

    @Override
    public Result updatePurchaseState(String id) {
        Purchase purchase = new Purchase();
        purchase.setState(3);
        purchase.setId(id);
        int i = this.purchaseMapper.updateById(purchase);
        return i > 0 ? new Result(CodeMsg.SUBMIT_APPLICATION_SUCCESS) : new Result(CodeMsg.SUBMIT_APPLICATION_ERROR);
    }

    @Override
    public Result submitPurchaseState(String[] ids, Integer[] states) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.SUBMIT_APPLICATION_ERROR);
        }

        for (Integer state : states) {
            if (state == 1 || state == 3 || state == 4 || state == 6) {
                return new Result(CodeMsg.SUBMIT_APPLICATION_TWO_ERROR);
            }
        }

        Purchase purchase = new Purchase();
        purchase.setState(3);
        this.purchaseMapper.update(purchase, new LambdaQueryWrapper<Purchase>()
                .in(Purchase::getId, ids));
        return new Result(CodeMsg.SUBMIT_APPLICATION_SUCCESS);
    }

    @Override
    public Result toVoidPurchaseState(String[] ids, Integer[] states) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.TO_VOID_ERROR);
        }

        for (Integer state : states) {
            if (state == 1 || state == 4 || state == 6) {
                return new Result(CodeMsg.TO_VOID_ERROR);
            }
        }

        Purchase purchase = new Purchase();
        purchase.setState(6);
        this.purchaseMapper.update(purchase, new LambdaQueryWrapper<Purchase>()
                .in(Purchase::getId, ids));
        return new Result(CodeMsg.TO_VOID_SUCCESS);
    }

    @Override
    public PageResult selectAllPurchasePageByState(PurchaseVo purchaseVo) {
        Page<Purchase> page = new Page<>(purchaseVo.getPage(), purchaseVo.getLimit());
        IPage<Purchase> purchaseIPage = this.purchaseMapper.selectPage(page, new LambdaQueryWrapper<Purchase>()
                .eq(purchaseVo.getSupplierId() != null, Purchase::getSupplierId, purchaseVo.getSupplierId())
                .like(StringUtils.hasText(purchaseVo.getApplicant()), Purchase::getApplicant, purchaseVo.getApplicant())
                .eq(purchaseVo.getState() != null, Purchase::getState, purchaseVo.getState())
                .in(Purchase::getState, Arrays.asList(3, 4, 5))
                .orderByDesc(Purchase::getCreateTime));

        List<Purchase> records = purchaseIPage.getRecords();
        //取出供应商名称
        for (Purchase record : records) {
            Integer supplierId = record.getSupplierId();
            Supplier supplier = supplierService.selectSupplierById(supplierId);
            record.setSupplierName(supplier.getName());
        }
        return new PageResult(purchaseIPage.getTotal(), records);
    }

    @Override
    public Result updatePurchaseState(Purchase purchase, String[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.EXAMINE_ERROR);
        }
        this.purchaseMapper.update(purchase, new LambdaQueryWrapper<Purchase>()
                .in(Purchase::getId, ids));
        return new Result(CodeMsg.EXAMINE_SUCCESS);
    }

    @Override
    public Result submitPurchase(String[] ids, Integer[] states) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.SUBMIT_PURCHASE_ERROR);
        }

        for (Integer state : states) {
            if (state != 4) {
                return new Result(CodeMsg.SUBMIT_PURCHASE_TWO_ERROR);
            }
        }

        for (String id : ids) {
            List<Order> orders = this.orderMapper.selectList(new LambdaQueryWrapper<Order>()
                    .eq(Order::getPurId, id));
            if (orders == null) {
                return new Result(CodeMsg.SUBMIT_PURCHASE_ERROR);
            }
            for (Order order : orders) {
                //修改库存
                this.drugInfoMapper.updateDrugInfoNumber(order.getDrugId(), order.getNumber());
            }
            //入库成功后，修改订单状态
            Purchase purchase = new Purchase();
            purchase.setState(1);
            purchase.setId(id);
            this.purchaseMapper.updateById(purchase);
        }

        return new Result(CodeMsg.SUBMIT_PURCHASE_SUCCESS);
    }
}
